<template>
  <div>
    <TopFilter ref="topForm" :list="topFormList" :cols="4" @filterChange="handleSearch"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="partInventoryManagerPatrItemAdjustWorkOrderSelect"
      :height="420"
      :columns="tableColumns"
      :onColumnsChange="columns => (this.tableColumns = columns)"
      :fetchapi="fetchapi"
      :datakey="'rows'"
      :params="fetchParams"
      selectionType="single"
      :onRowSelectChange="tableSelectChange"
    ></FilterTable>
    <div class="btnWrap">
      <el-button size="small" @click="closeDialog">取消</el-button>
      <el-button size="small" type="primary" :disabled="disable_confirm" @click="handleConfirm">确定</el-button>
    </div>
  </div>
</template>

<script>
import { getWorkOrderlist, getAllReparType } from '@/api/part/inventoryManager/partPriceAdjust';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import { getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import { getUser, getUserId } from '@/assets/js/auth';
import dictCode from '@/utils/dictCode';
import { dictionary } from '@/utils/dictMixin';

export default {
  mixins: [dictionary],
  props: {
    workOrderCode: String
  },
  data() {
    return {
      topFormList: this.createTopFormList(),
      tableColumns: this.createTableColumns(),
      fetchapi: getWorkOrderlist,
      fetchParams: { xhrAbort: true },
      selectedWorkOrder: {}, // 选中的工单信息
      salerList: {}, // 销售列表
      allRepairType: {}, // 所有维修类型(回显用)
      addBtnCanClick: true,
      currentUserId: '',
      disable_confirm: false,
      allUserList: {} // 所有人员列表(翻译用)
    };
  },
  created() {
    this.queryAllRepairType();
    this.getAllUser();
  },
  mounted() {
    this.getCounselorList();
    if (this.workOrderCode) {
      this.topFormList.find(item => item.fieldName == 'roNo').initialValue = this.workOrderCode;
      this.handleSearch({
        roNo: this.workOrderCode
      });
    } else {
      this.fetchParams.xhrAbort = false;
    }
  },
  methods: {
    createTopFormList() {
      return [
        {
          type: 'INPUT',
          label: '工单号',
          fieldName: 'roNo'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license'
        },
        {
          type: 'SELECT',
          label: '服务顾问',
          fieldName: 'serviceAdvisor',
          filterable: true,
          itemList: []
        },
        {
          type: 'INPUT',
          label: '车主',
          fieldName: 'ownerName'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          sorter: true,
          dataIndex: 'customIndex',
          width: 70,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '工单号',
          width: 120,
          dataIndex: 'roNo',
          showOverflowTooltip: true
        },
        {
          title: '维修类型',
          dataIndex: 'repairTypeCode',
          render: params => {
            return <span>{this.allRepairType[params.row.repairTypeCode]}</span>;
          }
        },
        {
          title: '车牌号',
          dataIndex: 'license'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',align: 'center',
          width: 180,
          showOverflowTooltip: true
        },
        {
          title: '车主',
          dataIndex: 'ownerName'
        },
        {
          title: '服务顾问',
          dataIndex: 'serviceAdvisor',
          render: params => {
            return <span>{this.salerList[params.row.serviceAdvisor]}</span>;
          }
        },
        {
          title: '开单日期',
          dataIndex: 'roCreateDate',
          dateFormat: 'yyyy-MM-dd'
        },
        // {
        //   title: '锁定人',
        //   dataIndex: 'lockUser',
        //   render: params => {
        //     return <span>{this.allUserList[params.row.lockUser]}</span>;
        //   }
        // },
        {
          title: '竣工标识',
          dataIndex: 'completeTag',
          render: params => {
            return <span>{this.createDictText(params.row.completeTag, 1004)}</span>;
          }
        },
        // {
        //   title: '是否仓库关单',
        //   dataIndex: 'isCloseRo',
        //   render: params => {
        //     return <span>{this.createDictText(params.row.isCloseRo, 1004)}</span>;
        //   }
        // },
        {
          title: '预交车时间',
          dataIndex: 'endTimeSupposed',
          dateFormat: 'yyyy-MM-dd'
        }
        // {
        //   title: '销售顾问',
        //   dataIndex: 'consultant',
        //   render: params => {
        //     return <span>{this.allUserList[params.row.consultant]}</span>;
        //   }
        // },
        // {
        //   title: '配件销售单号',
        //   dataIndex: 'salesPartNo'
        // }
      ];
    },
    // 获取服务顾问列表
    async getCounselorList() {
      let roleParam = { role: dictCode.SERVICE_CONSULTANT };
      try {
        let res = await searchRoleByCode(roleParam);
        let obj = {};
        this.topFormList[2].itemList = res.data.map(x => {
          obj[x.userId] = x.employeeName;
          return { text: x.employeeName, value: x.userId };
        });
        this.salerList = obj;
      } catch (err) {
        console.log(err);
      }
    },
    // 获取全部人员列表
    async getAllUser() {
      let res = await getDealerUser();
      this.currentUserId = await getUserId();
      let obj = {};
      res.data.map(item => {
        obj[item.userId] = item.employeeName;
      });
      this.allUserList = obj;
    },
    // 点击查询按钮
    handleSearch(e) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, { ...e, xhrAbort: false });
    },
    // 关闭弹框
    closeDialog() {
      this.selectedWorkOrder = {};
      this.$emit('changeWorkOrderSelectDialog', false);
    },
    // 列表选择框变化
    tableSelectChange(e) {
      if (e.length) {
        this.disable_confirm = false;
      }
      this.selectedWorkOrder = e[0];
    },
    // 点击确定按钮
    handleConfirm() {
      this.disable_confirm = true;
      if (!this.selectedWorkOrder) {
        return this.$notify.warning({ title: '提示信息', message: '请选中一条数据' });
      }
      // let lockUser = this.selectedWorkOrder.lockUser;
      // if (lockUser != null && lockUser != '' && this.currentUserId != lockUser) {
      //   this.disable_confirm = false;
      //   return this.$notify.error({ title: '提示信息', message: '单据' + this.selectedWorkOrder.roNo + '已被其他用户锁定!' });
      // }

      this.$emit('onConfirm', this.selectedWorkOrder);
      this.closeDialog();
    },
    // 获取全部维修类型
    async queryAllRepairType() {
      let res = await getAllReparType();
      let obj = {};
      res.data.map(item => {
        obj[item.REPAIR_TYPE_CODE] = item.REPAIR_TYPE_NAME;
      });
      this.allRepairType = obj;
    }
  }
};
</script>

<style scoped>
.bottomBtn {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
